import { InjectionKey, App } from 'vue'
import { createStore, useStore as baseUseStore, Store } from 'vuex'
import { StateTypes } from './types'
import index from './modules/index'

// InjectionKey 将store安装到Vue应用程序时提供类型,将类型传递InjectionKey给useStore方法
// 定义注入类型
const key: InjectionKey<Store<StateTypes>> = Symbol()

const store = createStore<StateTypes>({
  modules: {
    index
  },
  // getters
})

export function useStore() {
  return baseUseStore(key)
}

export function setupStore(app: App<Element>): void {
  app.use(store, key)
}

export default store
